<?php

/**
 * Blueprint is a Template engine, aiming to be as fast as plain PHP, but allowing
 * developers and designers to be able to see what the page should look like without
 * PHP or in a WYSIWYG.
 * 
 * PHP versions 4 and 5
 * 
 * Licensed under The MIT License
 * 
 * @author Jacob Wright
 * @copyright		Copyright (c) 2006, Within Code LLC
 * @package			blueprint
 * @version			$Revision: $
 * @modifiedby		$LastChangedBy: $
 * @lastmodified	$Date: 2006-05-25 16:12:33 -0600 (Thu, 25 May 2006) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */

global $BLUEPRINT_TEMPLATES_DIR, $BLUEPRINT_CACHE_DIR, $BLUEPRINT_EXPIRES, $BLUEPRINT_WEBROOT, $BLUEPRINT_COMPONENTS_DIRS;
define('BLUEPRINT_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);


// START CONFIGURATION  --  EDIT THESE AS NEEDED

/*
This is the base directory from which all your templates will be loaded
The cache directory will mimick this directory's structure as it stores and
saves the cached PHP templates
*/
$BLUEPRINT_TEMPLATES_DIR = "";

/*
The directory where all the cache is stored (needs to be writable by the
webserver)
*/
$BLUEPRINT_CACHE_DIR = "";

/*
The default time in minutes a text-cached page will expire
*/
$BLUEPRINT_EXPIRES = 15;

/*
The webroot for the site
*/
$BLUEPRINT_WEBROOT = preg_replace('|index.php.*$|', "", $_SERVER['PHP_SELF']);

/*
all of the locations which Blueprint should look for components (it will
look in the order that they appear in the array)
*/
$BLUEPRINT_COMPONENTS_DIRS = array(
	BLUEPRINT_DIR . "components"
);

// END CONFIGURATION


include_once(BLUEPRINT_DIR . '/PHPTemplate.php');
include_once(BLUEPRINT_DIR . '/CachedTemplate.php');
include_once(BLUEPRINT_DIR . '/parsing/TemplateParser.php');


/**
 * Main template class
 * 
 * @author Jacob Wright
 * @package blueprint
 */
class Blueprint {
	
	// private variables
	
	/**
	 * An instance of PHPTemplate or CachedTemplate which Blueprint uses
	 * @var PHPTemplate
	 */
	var $template;
	var $templateFile;
	var $phpCache;
	
	/**
	 * Blueprint Constructor, creates a plain template or cached template
	 * 
	 * @param string $template The file name of the template file
	 * @param string or false $cachedName The unique name to identify the text cache of this template
	 * or false to denote a basic (uncached) template
	 */
	function Blueprint($templateFile, $cachedFile = false, $expires = null) {
		global $BLUEPRINT_CACHE_DIR, $BLUEPRINT_TEMPLATES_DIR, $BLUEPRINT_EXPIRES;
		if (!$expires)
			$expires = $BLUEPRINT_EXPIRES;
		
		$this->phpCache = $BLUEPRINT_CACHE_DIR . preg_replace('/\.[^.]*$/', '.php', $templateFile);
		$this->templateFile = $BLUEPRINT_TEMPLATES_DIR . $templateFile;
		
		// create or update the PHP version of this template
		TemplateParser::update($this->templateFile, $this->phpCache);
		
		// use PHPTemplate or CachedTemplate with the PHP version of the template file
		$this->template = $cachedFile 
			? new CachedTemplate($this->phpCache, $cachedFile, $expires)
			: new PHPTemplate($this->phpCache);
	}
	
	/**
	 * Adds a filter to be run on the template after it is processed
	 *
	 * @param string/array $function Function name or an array(obj $object, string $method)
	 */
	function addFilter($function) {
		$this->template->addFilter($function);
	}
	
	/**
	 * Determines whether the template's cache exists and is not outdated
	 * 
	 * @return boolean Whether or not the template's cache is valid
	 */
	function isCached() {
		if (method_exists($this->template, 'isCached'))
			return $this->template->isCached();
		return false;
	}
	
	/**
	 * Assign data to a variable name for the template.
	 * 
	 * @param string $name Name of the variable as it will be known inside the template
	 * @param mixed $value Data (string, int, object, array) that will be assigned to specified variable name
	 */
	function assign($name, $value) {
		return $this->template->assign($name, $value);
	}
	
	/**
	 * Unassigns an assigned variable name
	 * 
	 * @param string $name The name of the assigned variable being unassigned
	 */
	function unassign($name) {
		return $this->template->unassign($name);
	}
	
	/**
	 * Returns the value of an assigned variable in the template
	 * 
	 * @param string $name The variable name
	 * @return mixed The value of the variable being asked for
	 */
	function getVar($name) {
		return $this->template->getVar($name);
	}
	
	/**
	 * Assigns all the values in an associative array to corresponding variables in the template
	 * 
	 * @param array $vars An associative array with the name value pairs of the variables to be added
	 * @param boolean $clear An optional parameter which, if set to true, will clear all previously assigned variables
	 */
	function addVars($vars, $clear = false) {
		return $this->template->addVars($vars, $clear);
	}
	
	/**
	 * Returns an associative array of the variables assigned to this template
	 * 
	 * @return array An associative array of all variables in template
	 */
	function getVars() {
		return $this->template->getVars();
	}
	
	/**
	 * Processes the template and returns it's output
	 * 
	 * @return string The final output of the template
	 */
	function fetch() {
		return $this->template->fetch();
	}
	
	/**
	 * Processes the template and prints it's output to the screen
	 */
	function display() {
		return $this->template->display();
	}
}



if (!function_exists('mkdir_r')) {
	/**
	 * Recursively creates a directory
	 * 
	 * @param string $pathname The path of the directory
	 * @param int $mode The mode of the directories
	 */
	function mkdir_r($pathname, $mode = 0777) {
		if (!file_exists($pathname)) {
			mkdir_r(dirname($pathname), $mode);
			mkdir($pathname);
			chmod($pathname, $mode);
		}
	}
}


?>